package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    //课程列表数据查询
    @GetMapping("/courses")
    public Result getCourses(Course course) {
        PageResult<Course> coursesPage = courseService.listCourses(course);
        return Result.success(coursesPage);
    }

    //根据id删除课程
    @DeleteMapping("/courses/{id}")
    public Result deleteCourse(@PathVariable Integer id) {
        courseService.deleteCourse(id);
        return Result.success();
    }

    //添加课程
    @PostMapping("/courses")
    public Result addCourse(@RequestBody Course course) {
        courseService.addCourse(course);
        return Result.success();
    }

    //根据id查询课程
    @GetMapping("/courses/{id}")
    public Result getCourseById(@PathVariable Integer id) {
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }
    //修改课程
    @PutMapping("/courses")
    public Result updateCourse(@RequestBody Course course) {
        courseService.updateCourse(course);
        return Result.success();
    }

    //查询所有课程
    @GetMapping("/courses/list")
    public Result listCourses() {
        List<Course> courses = courseService.getAllCourses();
        return Result.success(courses);
    }

    //根据subject查询课程
    @GetMapping("/courses/subject/{subject}")
    public Result listCoursesBySubject(@PathVariable Integer subject) {
        List<Course> courses = courseService.listCoursesBySubject(subject);
        return Result.success(courses);
    }
}
